package com.easyskool.composer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;

import com.easyskool.constants.CommonConstants;
import com.easyskool.constants.TableNames;
import com.easyskool.dao.BaseDao;
import com.easyskool.exception.DaoException;
import com.easyskool.hibernate.model.ClassDetails;
import com.easyskool.hibernate.model.ModeratorDetails;
import com.easyskool.hibernate.model.SubjectDetails;
import com.easyskool.hibernate.model.TeacherDetails;
import com.easyskool.hibernate.model.WClass;
import com.easyskool.util.mailUtil;
import com.easyskool.utilfunction.NullObjects;
import com.easyskool.utilfunction.UtilFunctions;
import com.ivp.cloud.stratus.logger.LoggingUtil;

public class ModeratorComposer {
	private static LoggingUtil logger = (LoggingUtil) LoggingUtil
			.getLogger(WebadminComposer.class.getName());

	BaseDao baseDao;

	public BaseDao getBaseDao() {
		if (NullObjects.isNull(baseDao)) {
			baseDao = new BaseDao();
		}
		logger.info("exiting getBaseDao");
		return baseDao;
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param toInsert
	 * @return String
	 * @throws Exception
	 */
	public String createModeratorDetails(String user, String toInsert)
			throws Exception {
		// toInsert = UtilFunctions.insertParamAdd(user,toInsert);
		System.out.println("to insert " + toInsert);
		getBaseDao().insert(TableNames.moderatorDetails, toInsert);
		logger.info("exiting createMasterClass");
		JSONObject json = new JSONObject(toInsert);
		String mail = (String) json.get("mdrEmail");
		System.out
				.println(mail
						+ "mreddddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeail");
		String content=  "<a href='accessAuthorize.jsp'>Activate</a>";
		mailUtil ma=new  mailUtil(mail,"Moderator Account Created",content);
	//	ma.sendMail(mail,"Moderator Account Created","AC");
		return UtilFunctions.createJsonObject(CommonConstants.CREATE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return String
	 * @throws Exception
	 */
	public String getModeratorDetails(String user, String whereClause)
			throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		Map whereMap = mapper.readValue(whereClause, HashMap.class);
		List<WClass> wClasses = getBaseDao().find(TableNames.moderatorDetails,
				whereMap);
		String str = mapper.writeValueAsString(wClasses);
		logger.info("exiting getMasterClass");
		return str;
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @param updateClause
	 * @return String
	 * @throws Exception
	 */
	public String updateModeratorDetails(String user, String whereClause,
			String updateClause) throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		// updateClause = UtilFunctions.updateParamAdd(user, updateClause);
		Map whereMap = new HashMap<>();
		getBaseDao().update(TableNames.moderatorDetails, whereMap, updateClause);
		logger.info("exiting updateMasterClass");
		return UtilFunctions.createJsonObject(CommonConstants.UPDATE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return String
	 * @throws Exception
	 */
	public String deleteModeratorDetails(String user, String whereClause)
			throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		ModeratorDetails whereMap = mapper.readValue(whereClause,
				TableNames.moderatorDetails);
		getBaseDao().delete(whereMap);
		logger.info("exiting deleteMasterClass");
		return UtilFunctions.createJsonObject(CommonConstants.DELETE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------

	public String deleteSelectedModeratorDetails(String user, String whereClause)
			throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		ArrayList<Map> whereMap = mapper
				.readValue(whereClause, ArrayList.class);
		for (Map map : whereMap) {
			String str = mapper.writeValueAsString(map);
			ModeratorDetails moderatorDetails = mapper.readValue(str,
					TableNames.moderatorDetails);
			getBaseDao().delete(moderatorDetails);
		}
		logger.info("exiting deleteSelectedMasterSection");
		return UtilFunctions.createJsonObject(CommonConstants.DELETE,
				CommonConstants.SUCCESS);
	}

	public String getClassesAndSections(int schoolCode) {
		Map <String, Object> whereMap =new HashMap<String , Object>();
		whereMap.put("clsSchoolCode", schoolCode);
		System.out.println("Data Entering in the getClassesAndSections composer : "+schoolCode);
		String str="";
		try {
		List<ClassDetails> returnedClasses=	getBaseDao().find(ClassDetails.class, whereMap);

		ObjectMapper mapper = new ObjectMapper();
		try {
			  str = mapper.writeValueAsString(returnedClasses);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		} catch (DaoException e) {
			e.printStackTrace();
		}
		System.out.println("Data Exting the getClassesAndSections composer : "+str);
		return str;
	}
	public String getSchoolTeachers(int schoolCode) {
		Map <String, Object> whereMap =new HashMap<String , Object>();
		whereMap.put("tchCurrentSchoolCode", schoolCode);
		System.out.println("Data Entering in the getSchoolTeachers composer : "+schoolCode);
		String str="";
		try {
		List<TeacherDetails> returnedTeachers=	getBaseDao().find(TeacherDetails.class, whereMap);

		ObjectMapper mapper = new ObjectMapper();
		try {
			  str = mapper.writeValueAsString(returnedTeachers);
			  System.out.println("STRING IS: "+str);
			  str="[";
			  for(int i=0;i<returnedTeachers.size();i++)
			  {
				  TeacherDetails t= returnedTeachers.get(i);
				if(i==0)
				  str=str+"{\"teacherCode\":\""+t.getTeacherCode()+"\",\"teacherName\":\""+t.getTchFname()+" "+t.getTchLname()+"\"}";
				else
					str=str+",{\"teacherCode\":\""+t.getTeacherCode()+"\",\"teacherName\":\""+t.getTchFname()+" "+t.getTchLname()+"\"}";
			  }
			  str=str+"]";
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		} catch (DaoException e) {
			e.printStackTrace();
		}
		System.out.println("Data Exting the getSchoolTeachers composer : "+str);
		return str;
	}
public String getSubjectsForSchool(int schoolCode, String whereClause) throws JsonParseException, JsonMappingException, IOException {
	System.out.println("Data Entering in the getSubjectsForClass composer : "+schoolCode+" "+whereClause);
	String str="";
	try {
		  ObjectMapper mapper = new ObjectMapper();
		  Map whereMap = new HashMap<String,Object>();//mapper.readValue(whereClause, HashMap.class);
	whereMap.clear();
	System.out.println(whereMap);
	whereMap.put("suSchoolCode", schoolCode);
	List<SubjectDetails> returnedSubjects=	getBaseDao().find(SubjectDetails.class, whereMap);
	System.out.println("Data Size returned in returnesSubjects: "+returnedSubjects.size());
 	try {
		  str = mapper.writeValueAsString(returnedSubjects);
	} catch (JsonGenerationException e) {
		e.printStackTrace();
	} catch (JsonMappingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	} catch (DaoException e) {
		e.printStackTrace();
	}
	System.out.println("Data Exting the getClassesAndSections composer : "+str);
	return str;
}
}
